# Python 2.6.4
# Project Euler, Problem 133
# Copyright 2010 Talha Zaman

from mine import sieveprimes
def A(n):
    if (n%10) not in [1, 3, 7, 9]: return 0
    last, digit, num = n%10, 0, 0
    while True:
        num = n*table[last,(11-num%10)%10] + num
        while num%10 == 1: num, digit = num/10, digit+1
        if num == 0: break
    return digit
table = dict()
for i in [1, 3, 7, 9]:
    for j in range(10):
        table[i,(i*j)%10] = j
import psyco
psyco.full()
primes = sieveprimes(100000)
total = 0
for p in primes:
    a = A(p)
    if a: 
        while not a%2: a/=2
        while not a%5: a/=5
    if a!=1: total += p
print total #453647705
